﻿package  {
	import flash.events.*;
	import flash.net.*;
	import flash.events.EventDispatcher;
	
	public class XMLloader extends EventDispatcher {
		var a:Array = new Array();
		var util:Util;
		
		public function XMLloader() {
			var xmlLoader:URLLoader = new URLLoader();
			xmlLoader.addEventListener(Event.COMPLETE, showXML);
			xmlLoader.load(new URLRequest("http://www.folkdranouter.be/webservice.php"));
		}
		private function showXML(e:Event):void {
				
			XML.ignoreWhitespace = true; 
			trace("Aantal events zijn:");
			var d:XML = new XML(e.target.data);
			trace(d.events.event.length());
			
			var o:xmlArtiest;
			
			for (var i:Number=0; i < d.events.event.length(); i++) {
				o = new xmlArtiest();
				o.setTitle(d.events.event[i].title.text());
				o.setID(d.events.event[i].id.text());
				o.setdescription(d.events.event[i].description.text());
				o.setimagesid(d.events.event[i].images[0].image.id.text());
				o.setextension(d.events.event[i].images[0].image.extension.text());
				o.setstartdate(d.events.event[i].startdate.text());
				o.setstarttime(d.events.event[i].starttime.text());
				o.setenddate(d.events.event[i].enddate.text());
				o.setsendtime(d.events.event[i].endtime.text());
				o.setwebsite(d.events.event[i].website.text());
				o.setyoutube(d.events.event[i].youtube.text());
				o.setMyspace(d.events.event[i].myspace.text());
				o.settent(d.events.event[i].location.text());
				a.push(o);
				trace(o.getTitle() + " Image ID: " + o.getimagesid());
			}
			
			startUtil();
			
							
		}
		
		private function startUtil():void{
			
			util = new Util();
			util.addEventListener("INIT", update);
		}
		
		private function update(e:Event):void{
			util.removeEventListener("INIT", update);
			util.updateFromXML(a);
			util.addEventListener("Updated", sendEvent);
		}
		
		private function sendEvent(e:Event):void{
			dispatchEvent(new Event("Updated!"));
		}
		

	}
	
}
